package com.example.servlet;

import com.alibaba.fastjson2.JSON;
import com.example.entity.Pet;
import com.example.service.PetService;
import com.example.service.impl.PetServiceImpl;
import com.example.util.AjaxResult;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = "/deleteServlet")
public class DeleteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int petId = Integer.parseInt(req.getParameter("petId"));
        PetService petService = new PetServiceImpl();
        int result;
        resp.setCharacterEncoding("UTF-8");
        PrintWriter pw = resp.getWriter();
        if(StringUtils.isNotBlank(String.valueOf(petId))){
            result = petService.delete(petId);
            if(result > 0){
                pw.write(JSON.toJSONString(AjaxResult.success()));
            }else{
                pw.write(JSON.toJSONString(AjaxResult.error()));
            }
        }else{
            pw.write(JSON.toJSONString(AjaxResult.error("不合法")));
        }
        pw.flush();
        pw.close();
    }
}
